﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace AOP.ConsoleTest
{

    /// <summary>
    /// 请求参数验证
    /// </summary>
    public class RequestParamCheckService : IInterceptionBehavior
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="getNext"></param>
        /// <returns></returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate next)
        {
            Console.WriteLine("校验参数...........");
            //获取参数类
            UserInfo? userInfo = input.Inputs[0] as UserInfo;

            if (userInfo != null && userInfo.Password.Length < 6)
            {
                return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于6"));
            }
            else
            {
                Console.WriteLine("参数检测无误");
                return next().Invoke(input, next);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool WillExecute
        {
            get { return true; }
        }



    }
}
